import { ref } from 'vue'

export default function useBatchSelect() {
  const batchMode = ref(false)
  const selectedIds = ref(new Set())
  const showBatchMenu = ref(false)

  // 进入批量模式
  const enterBatchMode = () => {
    batchMode.value = true
    showBatchMenu.value = true
  }

  // 切换单个选择
  const toggleSelect = (id) => {
    if (selectedIds.value.has(id)) {
      selectedIds.value.delete(id)
    } else {
      selectedIds.value.add(id)
    }
  }

  // 全选/取消全选
  const toggleSelectAll = (resources) => {
    if (selectedIds.value.size === resources.length) {
      selectedIds.value.clear()
    } else {
      resources.forEach(item => selectedIds.value.add(item.id))
    }
  }

  // 退出批量模式
  const exitBatchMode = () => {
    batchMode.value = false
    showBatchMenu.value = false
    selectedIds.value.clear()
  }

  return {
    batchMode,
    selectedIds,
    showBatchMenu,
    enterBatchMode,
    toggleSelect,
    toggleSelectAll,
    exitBatchMode
  }
}